<template>
  <div class="login-contain">
    <van-form>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" @click="login">提交</van-button>
      </div>
    </van-form>

    <div class="text" @click="$router.push('/register')">没有账号？立即注册</div>
  </div>
</template>

<script>
import { login } from "@/api";
export default {
  data() {
    return {
      username: "",
      password: "",
    };
  },
  methods: {
    async login() {
      let data = await login({
        username: this.username,
        password: this.password,
      });
      let { status, message, token, refreshToken, userInfo } = data;
      this.$toast(message);
      if (!status) {
        this.$store.commit("setToken", token);
        this.$store.commit("setRefreshToken", refreshToken);
        this.$store.commit("setUserInfo", userInfo);
        let redirect = this.$route.query.redirect;
        this.$router.replace(redirect || "/home");
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.login-contain {
  font-size: 14px;

  .text {
    font-size: 16px;
    color: #b9b5b5;
    text-align: center;
  }
}
</style>
